package ${packPath}.controller;

import com.apache.action.ServiceActionSuper;
import com.apache.api.api.ApiService;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.rpc.common.JsonEntityParese;
import com.apache.rpc.common.XMLMapParse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.Map;

/**
 * description: Http或Https访问action
 * 统一restful请求入口
 */
@Controller
@RequestMapping("${reqName}/service/")
public class ${restfullClassName} extends ServiceActionSuper {

	@Autowired(required = false)
    @Qualifier("${reqName}Service")
	private ApiService ${reqName}Service;

	/**
     * 获取service接口实例
     */
     @Override
     protected ApiService getApiService(String beanId) {
         if (Validator.isNull(beanId) || "${reqName}Service".equalsIgnoreCase(beanId)) {
             return ${reqName}Service;
         } else {
             return (ApiService) SpringContextLoader.getBean(beanId);
         }
     }

     /**
      * 处理返回数据
      */
     @Override
     protected String getResultEntityMsg(ResultEntity entity, String paramType) {
         String result = null;
         if ("json".equalsIgnoreCase(paramType))
             result = JsonEntityParese.instance().beanToJson(entity);
         else
             result = JsonEntityParese.instance().toXml(entity);
         return result;
     }

     /**
      * 解析请求报文
      */
     @Override
     protected ParamsVo getXMLMapParse(String paramStr, Map<String, Object> headMap) {
         Map<String, Object> bodyMap = new HashMap<String, Object>();
         ParamsVo vo = new ParamsVo();
         XMLMapParse.instance().xmlGetData(paramStr, headMap, bodyMap);
         vo.setMethodKey(headMap.get("MethodCode").toString());
         vo.setParams(bodyMap);
         return vo;
     }

}
